package com.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crm.commons.domain.ResponseData;
import com.crm.service.ICrmMenuService;
import com.crm.vo.SysMenu;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author junxian,dou
 * @since 2024-11-28
 */
@RequestMapping("/crmMenu")
@Api(tags = "菜单")
@RestController
public class CrmMenuController extends BaseController {

    @Resource
    private ICrmMenuService menuService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:menu:list')")
    public ResponseData getList(){
        return ResponseData.success(menuService.getListToNodeTree(getUserInfo()));
    }

    /**
     * 查询所有菜单树信息
     * @return
     */
    @RequestMapping("/treeList")
    @PreAuthorize("hasAuthority('system:menu:list')")
    public ResponseData treeList(){
        List<SysMenu> listToNodeTree = menuService.getListToNodeTree();
        return ResponseData.success(listToNodeTree);
    }

    /**
     * 添加或者修改
     * @param sysMenu
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('system:menu:add')"+"||"+"hasAuthority('system:menu:edit')")
    public ResponseData save(@RequestBody SysMenu sysMenu){

        menuService.saveMenu(sysMenu);
        return ResponseData.success();
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('system:menu:query')")
    public ResponseData findById(@PathVariable(value = "id")Long id){
        SysMenu sysMenu = menuService.getMenuById(id);
        return ResponseData.success(sysMenu);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('system:menu:delete')")
    public ResponseData delete(@PathVariable(value = "id")Long id){
        menuService.deleteById(id);
        return ResponseData.success();
    }


    @GetMapping("/iconList")
    public ResponseData selectIconList(){

        return ResponseData.success(menuService.selectIconList());
    }

}
